﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.WorkerListDto;
using MVC系统mvc.Dto.WWHoursDto;
using Newtonsoft.Json;
using WMS_MAV;

namespace MVC系统mvc.Controllers
{
    /// <summary>
    /// 工人工时管理控制器
    /// </summary>
    public class WWHoursController : Controller
    {
        /// <summary>
        /// 配置信息
        /// </summary>
        protected readonly IConfiguration configuration;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration">配置信息</param>
        public WWHoursController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 工人工时管理--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult WWorkingHoursView()
        {
            return View();
        }

        /// <summary>
        /// 工人列表--视图
        /// </summary>
        /// <returns></returns>
        public IActionResult WorkerListView()
        {
            return View();
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        private string ExtractTokenFromHeader()
        {
            if (Request.Headers.TryGetValue("Authorization", out var tokenHeaderValues) && tokenHeaderValues.Count > 0)
            {
                string token = tokenHeaderValues.FirstOrDefault()?.TrimStart("Bearer ".ToCharArray());
                return !string.IsNullOrEmpty(token) ? token : null;
            }
            return null;
        }


        /// <summary>
        /// 添加工人工时信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> AddWWHoursInfo(AddWWHoursDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/CreatedWWorkingHours";
            string list = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }


        /// <summary>
        /// 工人工时数据分页展示列表
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetWWHoursList(ShowWWHoursDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/ShowWWorkingHours?WorkerNumber=" + dto.WorkerNumber +  "&Status=" + dto.Status + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 删除工人工时信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> DeleteWWHours(DeletedWWHoursDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/DeleteWWorkingHours";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }

        /// <summary>
        /// 反填工人工时信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReverseWWHours(ReverseFillingWWHoursDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/ReverseFillingWWorkingHours?WWHoursId=" + dto.WWHoursId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 修改工人工时信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdateWWHours(UpdateWWHoursDto objTwo)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/UpdateWWorkingHours";
            string list = JsonConvert.SerializeObject(objTwo);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

        /// <summary>
        /// 添加工人信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> AddWorkerList(AddWorkerListDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/CreatedWorkerList";
            string list = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }


        /// <summary>
        /// 工人数据分页展示列表
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> GetWorkerList(ShowWorkerListDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/ShowWorkerList?WorkerNumber=" + dto.WorkerNumber + "&WorkersName=" + dto.WorkersName + "&Status=" + dto.Status + "&PageIndex=" + dto.PageIndex + "&PageSize=" + dto.PageSize;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 删除工人信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> DeleteWorkerList(DeletedWorkerListDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/DeleteWorkerList";
            var response = await httpClientWrapper.PostAsync(url, dto.Data);
            return Json(response);
        }

        /// <summary>
        /// 反填工人信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpGet]
        public async Task<ActionResult> ReverseWorkerList(ReverseFillingWorkerListDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/ReverseFillingWorkerList?WorkerListId=" + dto.WorkerListId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        /// 修改工人信息
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdateWorkerList(UpdateWorkerListDto objTwo)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/UpdateWorkerList";
            string list = JsonConvert.SerializeObject(objTwo);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

        /// <summary>
        /// 修改工人状态
        /// </summary>
        /// <returns>JsonResult</returns>
        [HttpPost]
        public async Task<ActionResult> UpdateWorkerListStatus(UpdateStatusWorkerListDto dto)
        {
            string token = ExtractTokenFromHeader();
            if (token == null)
            {
                return BadRequest("Authorization header missing or invalid.");
            }
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = configuration["Url"] + "wwhours/api/WWHours/UpdateWorkerListStatus";
            string list = JsonConvert.SerializeObject(dto);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }

    }
}
